package plugin.dao;


import org.apache.ibatis.annotations.*;
import pojo.User;

import java.util.List;

/**
 * @author Feuler
 * @since 2020/11/10
 **/

@CacheNamespace
public interface UserMapper {

    /**
     * 查询所有用户信息，同时查询出每个用户关联的订单信息
     *
     * @return 结果集
     */
    List<User> findAll();

    /**
     * 查询所有用户，同时拆线呢每个用户关联的角色信息
     *
     * @return 结果集
     */
    List<User> findAllUserAndRole();

    /**
     * 添加用户
     *
     * @param user 用户对象
     */
    @Insert("insert into `user` values(#{id}, #{username})")
    void addUser(User user);

    /**
     * 更新用户
     *
     * @param user 用户对象
     */
    @Update("update `user` set username = #{username} where id = #{id}")
    void updateUser(User user);

    /**
     * 查询用户
     *
     * @return 结果集
     */
    @Select("select * from `user`")
    List<User> selectUser();

    /**
     * 删除用户
     *
     * @param user 用户对象
     */
    @Delete("delete from `user` where id = #{id}")
    void deleteUser(User user);

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return 查询结果
     */
    @Select("select * from `user` where id = #{id}")
    User findUserById(Integer id);

    /**
     * 查询用户及用户的订单 注解方式
     *
     * @return 结果集
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orders", column = "id", javaType = List.class,
                    many = @Many(select = "multiple.dao.OrderMapper.findOrderByUid")),
    })
    List<User> findUserAndOrderWithAnnotation();

    /**
     * 查询用户及用户的角色信息 注解方式
     *
     * @return 结果集
     */
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roles", column = "id", javaType = List.class,
                    many = @Many(select = "multiple.dao.RoleMapper.findRoleByUid")),
    })
    List<User> findUserAndRoleWithAnnotation();
}
